package com.c.ctgproject.ui.review.material

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.review.MaterialReviewEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.mode.ReviewEnum
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.initData
import com.c.ctgproject.utils.showToast
import com.c.ctgproject.wiget.dialog.InputDialog
import kotlinx.android.synthetic.main.activity_order_detail.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource

/**
 *作者:LiBW
 *创建日期: 2020/9/9
 *描述:物料审核 订单详情页面
 */
class MaterialOrderDetailActivity : BaseActivity(), InputDialog.InPutDialogCallBack {
    private val mViewModel: MaterialOrderDetailViewModel by lazy { ViewModelProvider(this)[MaterialOrderDetailViewModel::class.java] }
    private val dialog: InputDialog by lazy { InputDialog(this) }
    private var type = MyConstants.AGREE

    private val data: MaterialReviewEntity.DataBean.ListBean by lazy { intent.extras!!.get(MyConstants.ENTITY) as MaterialReviewEntity.DataBean.ListBean }
    private val pageType: Int by lazy { intent.getIntExtra(MyConstants.AGREE_TYPE, 100) }

    private var materialNum=""

    override fun event() {
        changeTransparentStatusBar()
        changeStatusIconColor(true)

        //未审核页面有按钮但是没有审核意见
        when (pageType) {
            MyConstants.UN_REVIEWED -> {
                mButtonLL.visibility = View.VISIBLE
                mReviewMarkRL.visibility = View.GONE
            }
            else -> {
                mButtonLL.visibility = View.GONE
                mReviewMarkRL.visibility = View.VISIBLE
                mReviewMarkTV.text = data.auditOpinion.handlerNull()
            }
        }

        //是否为提供给销售页面的展示页面(不提供按钮功能)
        val showType = intent.getBooleanExtra(MyConstants.JUST_SHOW, false)
        if (showType) {
            mButtonLL.visibility = View.GONE
        }
        mOrderID.text = data.materialSalesNo.handlerNull()
        mTimeTV.text = data.createTime.handlerNull()
        mSellerTV.text = data.userName.handlerNull()
        when (data.isPass) {
            ReviewEnum.UN_REVIEWED.code -> {
                mOrderStatusTV.text = ReviewEnum.UN_REVIEWED.statueName
                mOrderStatusTV.textColorResource = ReviewEnum.UN_REVIEWED.color
            }
            ReviewEnum.AGREE.code -> {
                mOrderStatusTV.text = ReviewEnum.AGREE.statueName
                mOrderStatusTV.textColorResource = ReviewEnum.AGREE.color
            }
            ReviewEnum.REFUSE.code -> {
                mOrderStatusTV.text = ReviewEnum.REFUSE.statueName
                mOrderStatusTV.textColorResource = ReviewEnum.REFUSE.color
            }
        }
        MyLog.d("过来的数据:${data.materialName}")
        mPartNameTV.text = data.materialName.handlerNull()
        mSQYXSSL.text = data.planSalesNumber.handlerNull()
        mKCSL.text = data.materialInventoryNumber.handlerNull()
        mSQYJSSZL.text = data.planSalesWeight.handlerNull()
        mKCZL.text = data.materialInventoryWeight.handlerNull()
        mSXDJ.text = data.salesPrice.handlerNull()
        mGGJZL.text = data.remark.handlerNull()
        mGHDYGR.text = data.purchaser.handlerNull()
        mAddressTV.text = data.address.handlerNull()
        mHTH.text = data.contractNo.handlerNull()
        mSWDJH.text = data.identity.handlerNull()
        mPhoneNumTV.text = data.phoneNumber.handlerNull()
        mCWJSFS.text = data.financialSettlementWay.handlerNull()
        materialNum=data.materialSalesNo.handlerNull()
        //确认订单回调
        mViewModel.confirmOrder.observe(this, Observer {
            loading.dismiss()
            MyLog.d("订单确认回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                showToast("提交成功")
                finish()
            } else {
                showToast(data.msg)
            }
        })

        mViewModel.error.observe(this, Observer {
            MyLog.d("失败回调:$it")
            loading.dismiss()
            showToast(it)
        })

    }

    override fun click() {
        //确定
        mConfirmRL.setOnClickListener {
            if (data.materialSalesOrderId.toString().isEmpty()) {
                showToast("订单ID为空无法提交")
                return@setOnClickListener
            }
            dialog.show(supportFragmentManager, "dialog")
            type = MyConstants.AGREE
        }
        //拒绝
        mCancelBTN.setOnClickListener {
            if (data.materialSalesOrderId.toString().isEmpty()) {
                showToast("订单ID为空无法提交")
                return@setOnClickListener
            }
            type = MyConstants.REFUSE
            dialog.show(supportFragmentManager, "dialog")
        }
        //查看照片
        mCheckPhotoTV.setOnClickListener {
            if (materialNum.isNotEmpty()){
                startActivity<CheckPhotoActivity>(MyConstants.MATERIAL_NUM to materialNum)
            }else{
                showToast("未知错误,请稍后再试")
            }

        }
    }

    //审核意见回调页面
    override fun dialogResultText(msg: String) {
        loading.show()
        dialog.dismiss()
        mViewModel.orderConfirm(msg, type, data.materialSalesOrderId)
        MyLog.d("输入的内容$msg")
    }
    override fun getLayout() = R.layout.activity_order_detail
}